//
//  MLAChoosePhotosView.m
//  MagicLampApp
//
//  Created by Zheng on 2018/12/20.
//  Copyright © 2018 zheng.magic.lamp.app. All rights reserved.
//

#import "MLAChoosePhotosView.h"
#import "MLAChoosePhotosCell.h"

@interface MLAChoosePhotosView () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@end

@implementation MLAChoosePhotosView

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        _imagesArr = @[];
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        self.collectionViewLayout = flowLayout;
        self.backgroundColor = [UIColor clearColor];
        self.delegate = self;
        self.dataSource = self;
        self.scrollEnabled = NO;
        [self registerClass:[MLAChoosePhotosCell class] forCellWithReuseIdentifier:kPhotosCellID];
        [self registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    }
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section
{
    if(_imagesArr.count+1 > maxLength) {
        return maxLength;
    } else {
        return _imagesArr.count+1;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MLAChoosePhotosCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kPhotosCellID forIndexPath:indexPath];
    cell.itemIndex = indexPath.item;
    @weakify(self);
    cell.deletePhotosClick = ^(NSInteger index) {
        @strongify(self);
        if (self->_photosViewDidClickDetele) {
            self->_photosViewDidClickDetele(index,self.viewTag);
        }
    };

    if(_imagesArr.count+1 > maxLength) {
        cell.image = _imagesArr[indexPath.item];
        cell.isAdd = NO;
    } else {
        if (indexPath.row == _imagesArr.count) {
            cell.isAdd = YES;
        } else {
            cell.isAdd = NO;
            cell.image = _imagesArr[indexPath.item];
        }
    }
    
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), 15);
    titleLbl.textColor = CX_GetColorSubTitle();
    titleLbl.font = CX_GetNormalFont(14);
    titleLbl.text = _title;
    [headerView addSubview:titleLbl];
    return headerView;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(CGRectGetWidth(self.frame), CX_GetXScale(25));
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((CGRectGetWidth(self.frame)-20)/3., CX_GetXScale(80));
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_notSelect) {
        return;
    }
    
    MLAChoosePhotosCell *cell = (MLAChoosePhotosCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    if (cell.isAdd && _photosViewDidClickAdd) {
        _photosViewDidClickAdd();
    }
}

- (void)setImagesArr:(NSArray<UIImage *> *)imagesArr {
    _imagesArr = imagesArr;
    [self reloadData];
}

@end
